#include "loginwidget.h"
#include "ui_loginwidget.h"
#include"clientsocket.h"
#include"myapp.h"
#include<QFile>
#include<QMessageBox>
#include<QJsonObject>
LoginWidget::LoginWidget(QWidget *parent)
    : CustomMoveWidget (parent)
    , ui(new Ui::LoginWidget)
{
    ui->setupUi(this);
    myHelper::FormInCenter(this);
     ui->stackedWidget_2->setCurrentIndex(0); // 确保从第0页开始
    this->setWindowFlags(Qt::FramelessWindowHint);//去掉widget窗口标题栏，遗忘点
    this->setAttribute(Qt::WA_TranslucentBackground);//widget窗口透明生效

    QFile file("://resource/qss/default.css");
    file.open(QIODevice::ReadOnly);

    qApp->setStyleSheet(file.readAll());

    file.close();
    ui->lineEditPasswd->SetIcon(QPixmap("://resource/common/ic_lock.png"));
    ui->lineEditUser->SetIcon(QPixmap("://resource/common/ic_user.png"));
    ui->labelWinTitle->setText("用户登录");
    m_tcpSocket=new ClientSocket();
    connect(m_tcpSocket,&ClientSocket::signalStatus,this,&LoginWidget::onSignalStatus);
  connect(m_tcpSocket, &ClientSocket::signalMessage, this, &LoginWidget::onSignalMessage);

        m_tcpSocket->CheckConnected();


}

LoginWidget::~LoginWidget()
{
    delete ui;
}

void LoginWidget::on_btnWinMenu_clicked()
{
   ui->stackedWidget_2->setStartVal(0);
   ui->stackedWidget_2->setEndVal(180);
   ui->stackedWidget_2->animation(1);

}


void LoginWidget::on_btnCancel_clicked()
{
    ui->stackedWidget_2->setStartVal(0);
    ui->stackedWidget_2->setEndVal(180);
    ui->stackedWidget_2->animation(0);
}


void LoginWidget::on_btnWinClose_2_clicked()
{
    this->close();
}


void LoginWidget::on_btnWinClose_clicked()
{
    this->close();
}


void LoginWidget::on_btnLogin_clicked()
{   //检查是否连接到服务器
    //登录按钮
    //输入框内容转json文件
    //发送json文件给服务端
    QString username = ui->lineEditUser->text().trimmed();
    QString password = ui->lineEditPasswd->text();

    // 输入验证
    if (username.isEmpty()) {
        QMessageBox::warning(this, "登录", "请输入用户名");
        return;
    }
    if (password.isEmpty()) {
        QMessageBox::warning(this, "登录", "请输入密码");
        return;
    }

    // 显示登录中状态

    ui->btnLogin->setEnabled(false);

    // 构建登录数据
    QJsonObject json;
    json.insert("name", username);
    json.insert("passwd", password);

    // 发送登录请求
    m_tcpSocket->SltSendMessage(Login, json);

    qDebug() << "发送登录请求，用户名:" << username;
}
void LoginWidget::onSignalStatus(const quint8 &state)
{
    qDebug() << "收到状态信号:" << state;

    switch(state)
    {
    case ConnectedHost:
        ui->labelWinTitle->setText("已连接到服务器");
        qDebug() << "连接服务器成功";
        break;

    case DisConnectedHost:
        ui->labelWinTitle->setText("连接已断开");
        ui->btnLogin->setEnabled(true);
        qDebug() << "与服务器断开连接";
        break;

    case LoginSuccess:
    {
        qDebug() << "用户登录成功";
        ui->labelWinTitle->setText("登录成功");

        // 登录成功，跳转到主窗口
        MainWindow *mw = new MainWindow;
        mw->getsocket(this->m_tcpSocket,ui->lineEditUser->text());
        mw->show();
        this->hide();
        QJsonValue dataVals="用户已登录";
        m_tcpSocket->SltSendMessage(1,dataVals);
    }
    break;

    case LoginPasswdError:
    {
        qDebug() << "密码错误";
        ui->labelWinTitle->setText("密码错误");
        ui->btnLogin->setEnabled(true);
        QMessageBox::warning(this, "登录失败", "用户名或密码错误");

    }
    break;

    case LoginRepeat:
    {
        qDebug() << "重复登录";

        ui->btnLogin->setEnabled(true);
        QMessageBox::warning(this, "登录失败", "该账号已在其他地方登录");
    }
    break;

    case RegisterOk:
        qDebug() << "注册成功";
        break;

    case RegisterFailed:
        qDebug() << "注册失败";
        break;

    default:
        qDebug() << "未知状态:" << state;
        break;
    }
}

void LoginWidget::onSignalMessage(const quint8 &type, const QJsonValue &dataVal)
{
    // qDebug() << "收到消息类型:" << type << "数据:" << dataVal;

    // // 这里可以处理其他类型的消息
    // switch(type) {
    // case UserOnLine:
    //     qDebug() << "好友上线:" << dataVal;
    //     break;
    // case UserOffLine:
    //     qDebug() << "好友下线:" << dataVal;
    //     break;
    // default:
    //     qDebug() << "未处理的消息类型:" << type;
    //     break;
    // }
}



